# -*- coding: utf-8 -*-
"""
Created on Sun Nov 14 10:22:41 2021

@author: 刘长奇-2019300677
"""
from sklearn.ensemble import RandomForestClassifier 
import numpy as np
num,q1,q2,q3,q4,q5,q6,q7,q8,q9,q10,q11,q12,q13,q14,q15,q16,q17,q18,q19,q20,q21,q22,q23,q24,q25,q26,q27,q28,q29,q30,q31,q32,q33,q34,q35,q36,y= np.loadtxt("train.csv",delimiter=',', usecols=(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37), 
	unpack=True)
num = num.reshape(-1,1)
y = y.reshape(-1,1)
for i in range(1,37):
    exec ("q%s=q%s.reshape(-1,1)"%(i,i))
train = np.hstack((q1.reshape(-1,1),q2.reshape(-1,1)))
for i in range(3,37):
    exec ("train=np.hstack((train,q%s.reshape(-1,1)))"%i)
#产生训练集


# 建立随机森林模型
rfc = RandomForestClassifier(n_estimators=10, random_state=0)
rfc = rfc.fit(train,y)       
#用训练集数据训练模型 

y_pred=[]
j=0
for i in range(np.shape(train)[0]):
    y_pred.append(rfc.predict([train[i]]))
#为训练数据贴标签

from sklearn.metrics import confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt
y_pred=np.array(y_pred)
confusion_matrix_result=confusion_matrix(y,y_pred)
plt.figure(figsize=(10,6))
sns.heatmap(confusion_matrix_result,annot=True,cmap='Blues')
plt.xlabel('Predicted labels')
plt.ylabel('True labels')
plt.show()
#混淆矩阵将错误数据可视化
#相较于神经网络，随机森林的分类错误数据变少了，准确率提升了





